import { AgentType } from "./util/friendData";
import { GCtrl } from "./util/GCtrl";


class ViewMgr {

    private constructor() {
    };
    private static m_pInstance: ViewMgr = null;
    public static getInstance() {
        if (!this.m_pInstance) {
            this.m_pInstance = new ViewMgr();
        }
        return this.m_pInstance;
    }

    /**
     * 
     * @param serviceData ...{
     * type: 2:银行卡充值 3:全国招商 4:支付宝相关 5:游戏相关
     * Id: 客服ID
     * name: 会话名称
     * uiPath: UI路径
     * bank_name: 银行名
     * bank_code: 银行卡号
     * bank_owner: 开户人
     * }
     * 银行卡信息item路径cc.vv.GLConfig.chongZhiInfoItem_path
     */
    /**
     * 打开客服聊天界面
     * @param serviceType 客服类型
     * @param param 打开客服聊天的自定义参数
     */
    public OpenSerViceChatView(serviceType: number, param: any = null) {
        let serviceName = GCtrl.dataConfig.SERVICE_NAME[serviceType];
        if (!serviceName) return false;
        cc.vv.GLUtils.sendRequest('CustomerInfo', { type: serviceType }, function (err, data) {
            if (data.errcodedes) {
                cc.vv.GLUtils.showTips(data.errcodedes);
            } else {
                let serviceInfo: any = { isService: true, Id: data.rid, serviceName, type: serviceType, serviceType };
                cc.vv.chatMgr.updateServiceRid(data.rid, serviceName, "", serviceType);
                if (serviceType == GCtrl.dataConfig.SERVICE_TYPE.bank) {
                    cc.vv.GLUtils.sendRequest('CustomerAccount', {}, function (err, bankData) {
                        if (bankData.errcodedes) {
                            cc.vv.GLUtils.showTips(bankData.errcodedes);
                        } else {
                            for (let i in serviceInfo) bankData[i] = serviceInfo[i];
                            bankData.uiPath = cc.vv.GLConfig.chongZhiInfoItem_path;
                            GCtrl.eventCustom.emit('openChat', {
                                friendinfo: bankData,
                                from: 0
                            }, param);
                        }
                    }.bind(this));
                    return;
                }
                GCtrl.eventCustom.emit('openChat', {
                    friendinfo: serviceInfo,
                    from: 0
                }, param);
            }
        }.bind(this));
        return true;
    }

    // public OpenMyAgentView(agreeCallback: Function) { 
    //     let agentType = GCtrl.session.getType();
    //     //打开代理界面
    //     if (agentType == AgentType.playerWithClub || agentType == AgentType.playerWithoutClub)
    //         cc.vv.eventCustom.emit('GetClubPermissions', function (data) {
    //             let state = agentData.GetApplyAgentState();
    //             switch (state) {
    //                 case ApplyAgentState.NoPermission: {
    //                     cc.vv.GLUtils.showAlert1("您还没有建俱乐部权限，请申请权限", function () {
    //                         cc.vv.UIMgr.open(cc.vv.GLConfig.applyForAgent_path);
    //                     }.bind(this), function () { });
    //                 } break;
    //                 case ApplyAgentState.Checking: {
    //                     cc.vv.GLUtils.showAlert1("您的代理申请正在审核中，请耐心等待", function () {
    //                         cc.vv.UIMgr.open(cc.vv.GLConfig.applyState_path, null, null, data);
    //                     }.bind(this), function () { });
    //                 } break;
    //                 case ApplyAgentState.Agree: {
    //                     cc.vv.eventCustom.emit('loadAgentData');
    //                     agreeCallback && agreeCallback();
    //                 } break;
    //                 case ApplyAgentState.Refuse: {
    //                     cc.vv.GLUtils.showAlert1('您的申请被拒绝了, 是否重新提交申请', function () {
    //                         cc.vv.UIMgr.open(cc.vv.GLConfig.applyForAgent_path);
    //                     }.bind(this));
    //                 } break;
    //             }
    //         }.bind(this));
    //     else
    //         cc.vv.UIMgr.open(cc.vv.GLConfig.agent_path);
    // }
}
export let viewMgr: ViewMgr = ViewMgr.getInstance();
